package com.sport.video.service.config;

import cn.hutool.core.date.DateUtil;
import com.sport.video.common.dto.common.ResponseParams;
import com.sport.video.common.enums.RespEnum;
import com.sport.video.common.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Description: 全局异常处理流程
 * @author fugl
 * @date 2018年2月12日
 */
@ControllerAdvice //如果返回的为json数据或其它对象，添加该注解
@ResponseBody
public class GlobalExceptionHandler {//添加全局异常处理流程，根据需要设置需要处理的异常

	@ExceptionHandler(value = { Exception.class })
	public Object globalHandler(Exception exception) {
		ResponseParams<String> res = new ResponseParams<String>();
		res.setRespCode(RespEnum.ERROR.code);
		res.setRespMsg(RespEnum.ERROR.msg);
		res.setParam(exception.getMessage());
		res.setTimestamp(DateUtil.current(false));
		return res;
	}

	@ExceptionHandler(value = { BusinessException.class })
	public Object globalHandler(BusinessException businessException) {
		ResponseParams<String> res = new ResponseParams<String>();
		res.setRespCode(RespEnum.ERROR.code);
		res.setRespMsg(RespEnum.ERROR.msg);
		res.setParam(businessException.getMessage());
		res.setTimestamp(DateUtil.current(false));
		return res;
	}
}